package com.ksd.modules.note.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ksd.common.enums.StatusEnums;
import com.ksd.common.po.BatchIdsPO;
import com.ksd.common.web.threadlocal.SystemIdThrealLocal;
import com.ksd.commons.BaseServiceImpl;
import com.ksd.modules.note.entity.AdminNoteCommentReply;
import com.ksd.modules.note.mapper.AdminNoteCommentReplyMapper;
import com.ksd.modules.note.po.AdminNoteCommentReplySavePO;
import com.ksd.modules.note.po.AdminNoteCommentSearchPO;
import com.ksd.modules.note.service.IAdminNoteCommentReplyService;
import com.ksd.modules.note.vo.AdminNoteCommentReplyVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class AdminNoteCommentReplyServiceImpl extends BaseServiceImpl<AdminNoteCommentReplyMapper, AdminNoteCommentReply> implements IAdminNoteCommentReplyService {
    /**
     * 查询列表
     * @param adminNoteCommentSearchPO
     * @return
     */
    public Page<AdminNoteCommentReplyVo> findAdminNoteCommentReplys(AdminNoteCommentSearchPO adminNoteCommentSearchPO){
        Page<AdminNoteCommentReply> page = new Page<>(adminNoteCommentSearchPO.getPageNo(),adminNoteCommentSearchPO.getPageSize());
        LambdaQueryWrapper<AdminNoteCommentReply> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AdminNoteCommentReply::getSystemId,SystemIdThrealLocal.get().getSystemId());
        lambdaQueryWrapper.eq(Objects.nonNull(adminNoteCommentSearchPO.getParentId()), AdminNoteCommentReply::getParentId,adminNoteCommentSearchPO.getParentId());
        // 资源id
        lambdaQueryWrapper.eq(Objects.nonNull(adminNoteCommentSearchPO.getOpid()), AdminNoteCommentReply::getOpid,adminNoteCommentSearchPO.getOpid());
        // 用户唯一标识
        lambdaQueryWrapper.eq(StringUtils.isNotEmpty(adminNoteCommentSearchPO.getUuid()), AdminNoteCommentReply::getUuid,adminNoteCommentSearchPO.getUuid());
        lambdaQueryWrapper.eq(Objects.nonNull(adminNoteCommentSearchPO.getStatus()), AdminNoteCommentReply::getStatus,adminNoteCommentSearchPO.getStatus());

        // 模糊匹配
        if(StringUtils.isNotEmpty(adminNoteCommentSearchPO.getKeyword())){
            lambdaQueryWrapper.and(c -> c.like(
                            AdminNoteCommentReply::getUsername, adminNoteCommentSearchPO.getKeyword())
                    .or().like(AdminNoteCommentReply::getUuid, adminNoteCommentSearchPO.getKeyword())
                    .or().like(AdminNoteCommentReply::getTitle, adminNoteCommentSearchPO.getKeyword())

            );
        }
        // 排序
        lambdaQueryWrapper.orderByAsc(AdminNoteCommentReply::getStatus).orderByDesc(AdminNoteCommentReply::getCreateTime);
        Page<AdminNoteCommentReply> adminNotePage = this.page(page, lambdaQueryWrapper);
        Page<AdminNoteCommentReplyVo> adminNoteVoPage = tranferPageBo(adminNotePage, AdminNoteCommentReplyVo.class);
        return adminNoteVoPage;
    }



    /**
     * 保存和修改
     * @param adminNoteCommentReplySavePO
     * @return
     */
    @Transactional(rollbackFor=Exception.class)
    public AdminNoteCommentReplyVo saveUpdateAdminNoteCommentReply(AdminNoteCommentReplySavePO adminNoteCommentReplySavePO){
        AdminNoteCommentReply adminNote = tranferBo(adminNoteCommentReplySavePO, AdminNoteCommentReply.class);
        adminNote.setSystemId(SystemIdThrealLocal.get().getSystemId());
        adminNote.setSystemName(SystemIdThrealLocal.get().getSystemName());
        this.saveOrUpdate(adminNote);
        AdminNoteCommentReplyVo adminNoteVo = tranferBo(adminNote, AdminNoteCommentReplyVo.class);
        return adminNoteVo;
    }

    /**
     * 根据ID获取明细
     * @param id
     * @return
     */
    public AdminNoteCommentReplyVo getAdminNoteCommentReplyById(Long id){
        AdminNoteCommentReply adminNote = this.getById(id);
        AdminNoteCommentReplyVo adminNoteVo = tranferBo(adminNote, AdminNoteCommentReplyVo.class);
        return adminNoteVo;
    }

    /**
     * 单个物理删除
     * @return
     */
    @Transactional(rollbackFor=Exception.class)
    public Boolean removeAdminNoteCommentReplyById(Long id){
        boolean b = this.removeById(id);
        return b;
    }


    /**
     * 批量物理删除
     * @param batchIdsPO
     * @return
     */
    @Transactional(rollbackFor=Exception.class)
    public Boolean batchRemovesAdminNoteCommentReply(BatchIdsPO batchIdsPO){
        if (StringUtils.isEmpty(batchIdsPO.getIds())) {
            return false;
        }
        // 把文章分类进行恢复
        List<Long> ids = Arrays.stream(batchIdsPO.getIds().split(",")).map(Long::parseLong).collect(Collectors.toList());
        boolean b = this.removeByIds(ids);
        return b;
    }


    /**
     * 修改状态
     * @param id
     * @param column
     * @param value
     * @return
     */
    @Override
    @Transactional(rollbackFor=Exception.class)
    public boolean updateStatus(Long id,String column,Integer value){
        boolean b = super.updateStatus(id,column,value);
        return b;
    }

    /**
     * 修改状态
     * @param id
     * @param column
     * @param value
     * @return
     */
    @Override
    public boolean updateStatusStr(Long id,String column,String value){
        boolean b = super.updateStatusStr(id,column,value);
        return b;
    }

    /**
     * 批量上架
     *
     * @param batchIdsPO
     * @return
     */
    @Override
    public Boolean batchDowns(BatchIdsPO batchIdsPO) {
        if (StringUtils.isEmpty(batchIdsPO.getIds())) {
            return false;
        }

        // 把文章进行恢复
        List<Long> ids = Arrays.stream(batchIdsPO.getIds().split(",")).map(Long::parseLong).collect(Collectors.toList());
        UpdateWrapper<AdminNoteCommentReply> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("status", StatusEnums.UN_STATUS.getCode());
        updateWrapper.in("id", ids);
        return this.update(updateWrapper);
    }

    /**
     * 批量上架
     *
     * @param batchIdsPO
     * @return
     */
    @Override
    public Boolean batchUps(BatchIdsPO batchIdsPO) {
        if (StringUtils.isEmpty(batchIdsPO.getIds())) {
            return false;
        }

        // 把文章进行恢复
        List<Long> ids = Arrays.stream(batchIdsPO.getIds().split(",")).map(Long::parseLong).collect(Collectors.toList());
        UpdateWrapper<AdminNoteCommentReply> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("status", StatusEnums.STATUS.getCode());
        updateWrapper.in("id", ids);
        return this.update(updateWrapper);
    }

    @Override
    public Boolean removeByParentId(Long parentId) {
        LambdaQueryWrapper<AdminNoteCommentReply> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AdminNoteCommentReply::getParentId,parentId);
        return this.remove(lambdaQueryWrapper);
    }

    @Override
    public Boolean batchRemoveByParentId(List<Long> parentIds) {
        LambdaQueryWrapper<AdminNoteCommentReply> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(AdminNoteCommentReply::getParentId,parentIds);
        return this.remove(lambdaQueryWrapper);
    }

    @Override
    public Boolean updateStatusByParentId(Long parentId,String column,Integer value) {
        UpdateWrapper<AdminNoteCommentReply> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("parent_id",parentId);
        updateWrapper.set(column,value);
        return this.update(updateWrapper);
    }

    @Override
    public Boolean updateStatusByParentId(Long parentId,String column,String value) {
        UpdateWrapper<AdminNoteCommentReply> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("parent_id",parentId);
        updateWrapper.set(column,value);
        return this.update(updateWrapper);
    }

    @Override
    public Boolean batchUpdateStatusByParentIds(List<Long> parentIds,String column,Integer value) {
        UpdateWrapper<AdminNoteCommentReply> updateWrapper = new UpdateWrapper<>();
        updateWrapper.in("parent_id",parentIds);
        updateWrapper.set(column,value);
        return this.update(updateWrapper);
    }
}
